package com.ruoyi.system.coretools.apple;

import com.alibaba.fastjson2.JSONObject;
import lombok.Data;
import lombok.SneakyThrows;
import org.apache.commons.codec.binary.Base64;

import java.util.Map;

@Data
public class IdentityToken {

    //原始token
    private String token;

    //公钥id
    private String kid;

    //应用包名，应该为：com.test
    private String aud;

    //用户openId
    private String sub;

    @SneakyThrows
    public IdentityToken(String token) {
        this.token = token;
        String[] identityTokens = this.token.split("\\.");
        Map<String, Object> firstDate = JSONObject
                .parseObject(new String(Base64.decodeBase64(identityTokens[0]), "UTF-8"));
        Map<String, Object> secondData = JSONObject
                .parseObject(new String(Base64.decodeBase64(identityTokens[1]), "UTF-8"));

        this.kid = String.valueOf(firstDate.get("kid"));
        this.aud = String.valueOf(secondData.get("aud"));
        this.sub = String.valueOf(secondData.get("sub"));
    }
}
